package com.example.mybaiduweishi.fragments;

import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.fragments.base.BaseFragment;
import com.example.mybaiduweishi.services.MyLoadNewVersionService;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.vo.VersionInfo;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class UpdateVersionFragment extends BaseFragment {


    @BindView(R.id.update_version_text)
    TextView tv_Update;
    @BindView(R.id.update_btn_yes)
    Button updateBtnYes;
    @BindView(R.id.update_btn_no)
    Button updateBtnNo;
    @BindView(R.id.layout_updateversion_dialog)
    LinearLayout layoutUpdateversionDialog;
    Unbinder unbinder;
    VersionInfo versionInfo;

    public String getMsg() {
        Bundle bundle1 = getArguments();
        String msg = bundle1.getString("msg");
        return msg;
    }

    @Override
    public void initDataView(View view) {
        //更新TextView   getArgument
        Bundle bundle = getArguments();
        versionInfo = (VersionInfo) bundle.getSerializable("versioninfo");
        if (versionInfo != null) {
            tv_Update.setText(versionInfo.getInfo());
            //让取消的按钮显示出来
            updateBtnNo.setVisibility(View.VISIBLE);
        } else {
            tv_Update.setText(getMsg());
        }
    }

    @Override
    public int getContentXml() {
        return R.layout.dialog_version_activity;
    }

    //写一个方法构造UpdateVersionFragment对象  方法的重载
    public static UpdateVersionFragment getUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        updateVersionFragment.setArguments(bundle);//设置一个值以传递给UpdateActivity
        return updateVersionFragment;
    }

    //写一个方法构造UpdateVersionFragment对象
    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        String apkurl = versionInfo.getApkurl();
        bundle.putSerializable("versioninfo", versionInfo);
        updateVersionFragment.setArguments(bundle);//设置一个值以传递给UpdateActivity
        return updateVersionFragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder!=null) {
            unbinder.unbind();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.update_btn_yes, R.id.update_btn_no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.update_btn_yes:
                onClickedBtn_yes();
                break;
            case R.id.update_btn_no:
                dismiss();
                break;
        }
    }

    private void onClickedBtn_yes() {
        /**
         * 创建通知栏
         * 下载APK
         */
        //第一种启动服务的方式
//        Intent intent = new Intent();
//        intent.setClass(getActivity(), MyLoadNewVersionService.class);
//        getActivity().startService(intent);
//        //传下载地址
//        intent.putExtra("url",versionInfo.getApkurl());
//        MyApplication.getContext().startService(intent);
//        if(intent != null){
//            String apkUrl = intent.getStringExtra("downloadUrl");
//            //判断SD卡是否存在  能否读写 能写就写入SD卡，不能就写手机存储中
//            File downloadPath = MyUtil.getPath();
//        }
          //第二种
        if(versionInfo!=null) {
            Intent intent = new Intent(getActivity(), MyLoadNewVersionService.class);
            intent.putExtra("apkurl", versionInfo.getApkurl());
            MyApplication.getContext().startService(intent);
        }else {
            Toast.makeText(MyApplication.getContext(), "NONONONONO", Toast.LENGTH_SHORT).show();
        }
        dismiss();
    }
}

